package com.wlyuan.open.application.service;

import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.api.IOpenApiDocService;
import com.wlyuan.open.application.translator.OpenApiDocTranslator;
import com.wlyuan.open.domain.api.respository.OpenApiDocRepository;
import com.wlyuan.open.domain.api.respository.OpenApiRepository;
import com.wlyuan.open.domain.api.service.OpenApiService;
import com.wlyuan.open.domain.api.valueobj.OpenApiId;
import com.wlyuan.open.dto.api.OpenApiDocFilterDTO;
import com.wlyuan.open.vo.OpenApiDocDetailVO;
import com.wlyuan.open.vo.OpenApiDocVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

/**
 * @author lijing
 */
@DubboService
@Slf4j
@RequiredArgsConstructor
public class OpenApiDocServiceImpl implements IOpenApiDocService {
    private final OpenApiRepository openApiRepository;
    private final OpenApiService openApiService;
    private final OpenApiDocRepository openApiDocRepository;

    @Override
    public List<OpenApiDocVO> getApiDocList(OpenApiDocFilterDTO filter) {
        var list = openApiService.getApiDocList(filter);
        return OpenApiDocTranslator.translate(list);
    }

    @Override
    public OpenApiDocDetailVO getByApiId(Long apiId) {
        var api = openApiRepository.ofId(new OpenApiId(apiId));
        //接口不存在或接口状态不是启用
        if (api == null || !api.hasEnable()) {
            throw new ValueError("接口不存在");
        }
        return OpenApiDocTranslator.translate(openApiDocRepository.ofApiId(apiId), api.getName());
    }
}
